/*
  弹窗
  Toast 可以使用第三方库：
  https://pub.flutter-io.cn/packages/fluttertoast
 */

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  /*
    中间弹窗
   */
  void _alertDialog() async {
    var result = await showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text('提示信息'),
            content: Text('您即将开始下载'),
            actions: [
              // TextButton(onPressed: () {}, child: Text('确定')),
              TextButton(
                  onPressed: () {
                    Navigator.pop(context, 'cancel');
                  },
                  child: Text('取消'))
            ],
          );
        });
    // 打印弹窗返回的消息
    print('result is $result');
  }

  /*
    自定义弹窗
   */
  void _customDialog() async {
    await showDialog(
        context: context,
        builder: (ct) {
          return Dialog(
            backgroundColor: Colors.white,
            child: Container(
              width: 100,
              height: 100,
              child: Center(
                child: Text('验证码错误'),
              ),
            ),
          );
        });
  }

  /*
    底部弹窗
   */
  void _bottomDialog() async {
    var result = await showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 200,
            child: Column(
              children: [
                ListTile(
                  title: Text('分享A'),
                  onTap: () {
                    Navigator.pop(context, '分享成功');
                  },
                ),
                ListTile(
                  title: Text('分享B'),
                ),
                ListTile(
                  title: Text('分享C'),
                )
              ],
            ),
          );
        });
    print('result is $result');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Dialog'),
      ),
      body: Column(
        children: [
          TextButton(onPressed: _alertDialog, child: Text('中间弹窗')),
          TextButton(onPressed: _bottomDialog, child: Text('底部弹窗')),
          TextButton(onPressed: _customDialog, child: Text('自定义弹窗')),
        ],
      ),
    );
  }

  /// 设置底部弹窗不被键盘遮挡
  /// 1. 外层用 AnimatedPadding 包裹，设置 padding
  /// 2. isScrollControlled: true
  _showDialog() {
    showModalBottomSheet<bool>(
      context: context,
      builder: (BuildContext context) {
        return AnimatedPadding(
          padding: MediaQuery.of(context).viewInsets,
          duration: const Duration(milliseconds: 100),
          child: SizedBox(
            // height: ScreenUtil().screenHeight * 0.5,
            // color: Colors.white,
            child: TextField(
              maxLength: 100,
              maxLines: 5,
            ),
          ),
        );
      },
      isDismissible: false,
      isScrollControlled: true);
  }
}

